import Vuex from 'vuex'
import cookieParse from 'cookie-parse';

let store = () => new Vuex.Store({
    state: {
        token: ''
    },
    getters: {
        getToken: store => store.token,
    },
    mutations: {
        setToken(state, token) {
            state.token = token
        }
    },
    actions: {
        nuxtServerInit({ commit }, { req }) {
            let cookie = req.headers.cookie;
            let token = cookieParse.parse(cookie).token;
            commit('setToken', token);
        },
    }
})

export default store